<template>
  <div class="app-container">
    <h2 style="text-align: center">试卷列表</h2>

    <el-steps
      :active="3"
      process-status="wait"
      align-center
      style="margin-bottom: 40px"
    >
      <el-step title="填写课程基本信息" />
      <el-step title="创建课程大纲" />
      <el-step title="试卷列表" />
      <el-step title="发布课程" />
    </el-steps>
    <el-button type="text" @click="openPaperDialog()">新增试卷</el-button>
 <el-button type="text" @click="openAutoPaperDialog()">自动生成试卷</el-button>

    <ul class="chanpterList">
      <el-collapse accordion>
        <li v-for="(paper, index) in paperList" :key="paper.id">
          <el-collapse-item>
            <template slot="title">
              <p>
                {{ index + 1 }}：{{ paper.papertitle }}

                <span class="acts">

                  <el-button
                    style=""
                    @click="openTypedialog(paper.id)"
                    type="text"
                    >添加试题类型</el-button
                  >
                  <el-button type="text" @click="downloadPaperWord(paper.id)"
                    >生成word下载</el-button
                  >
                  <el-button type="text" @click="deletePaperById(paper.id)"
                    >删除</el-button
                  >
                </span>
              </p>
            </template>
            <ul class="chapterList videoList">
              <li v-for="e in paper.children" :key="e.id">
                <p>
                  {{ e.questionTypeSysName }}
                  <span class="acts">
                    <!-- <el-button style="" type="text" >编辑大题类型</el-button> -->
                    <el-button
                      style=""
                      type="text"
                      @click="openQuestionDialog(e)"
                      >查看试题列表</el-button
                    >
                    <el-button type="text" @click="deletePaperTypeById(e.id)"
                      >删除</el-button
                    >
                  </span>
                </p>
              </li>
            </ul>
          </el-collapse-item>
        </li>
      </el-collapse>
    </ul>

    <div>
      <el-button @click="previous">返回修改</el-button>
      <el-button type="primary" @click="next()">下一步</el-button>
    </div>

    <el-drawer
      title="习题添加"
      :visible.sync="addquestiondialog"
      :before-close="handleClose"
      direction="ltr"
      size="50%"
    >
      <div>
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li
                  v-for="(item, index) in subjectNestedList"
                  :key="index"
                  :class="{ active: oneIndex == index }"
                >
                  <a
                    :title="item.title"
                    href="#"
                    @click="searchOne(item.id, index)"
                    >{{ item.title }}</a
                  >
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li
                  v-for="(item, index) in subSubjectList"
                  :key="index"
                  :class="{ active: twoIndex == index }"
                >
                  <a
                    :title="item.title"
                    href="#"
                    @click="searchTwo(item.id, index)"
                    >{{ item.title }}</a
                  >
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <template>
          <el-table
            border
            :data="coursedata"
            style="width: 95%"
            max-height="500"
          >
            <el-table-column fixed prop="title" label="课程标题" width="200">
            </el-table-column>
            <el-table-column prop="lessonNum" label="课程数" width="60">
            </el-table-column>
            <el-table-column prop="viewCount" label="浏览数量" width="60">
            </el-table-column>
            <el-table-column prop="teacherId" label="教师" width="120">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="120">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="small"
                  @click="getcourseInfo(scope.row.id)"
                >
                  查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <el-drawer
          :title="''"
          :append-to-body="true"
          size="80%"
          :visible.sync="innerDrawer"
        >
          <template>
            <div v-if="queryQuestionByTypeSys.typeSys == 1">
              <el-table
                :data="choiceList"
                style="width: 100%"
                @selection-change="handleChoiceSelectionChange"
              >
                <el-table-column type="selection" width="55"> </el-table-column>
                <el-table-column label="序号" width="70" align="center">
                  <template slot-scope="scope">
                    {{ (choicepage - 1) * limit + scope.$index + 1 }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="content"
                  label="题干"
                  style=""
                  min-width="300"
                >
                </el-table-column>
                <el-table-column prop="answer" label="答案" width="100">
                </el-table-column>
                <el-table-column prop="choiceA" label="选项A" width="150">
                </el-table-column>
                <el-table-column prop="choiceB" label="选项B" width="150">
                </el-table-column>

                <el-table-column prop="choiceC" label="选项C" width="150">
                </el-table-column>
                <el-table-column prop="choiceD" label="选项D" width="150">
                </el-table-column>
                <el-table-column prop="difficulty" label="难度" width="90">
                </el-table-column>
                <el-table-column prop="useCount" label="引用次数" width="90">
                </el-table-column>
                <el-table-column prop="title" label="名称"> </el-table-column>
              </el-table>

              <!-- 分页 -->
              <el-pagination
                :current-page="this.choicepage"
                :page-size="limit"
                :total="this.choicetotal"
                style="padding: 30px 0; text-align: center"
                layout="total, prev, pager, next, jumper"
                @current-change="fetchChoiceQuestion"
              />
            </div>
            <div v-else>
              <el-table
                :data="questions"
                style="width: 90%"
                @selection-change="handlequestionSelectionChange"
              >
                <el-table-column type="selection" width="55"> </el-table-column>
                <el-table-column label="序号" width="70" align="center">
                  <template slot-scope="scope">
                    {{ (quesitonpage - 1) * limit + scope.$index + 1 }}
                  </template>
                </el-table-column>
                <el-table-column
                  prop="content"
                  label="题干"
                  style=""
                  min-width="400"
                >
                </el-table-column>
                <el-table-column prop="answer" label="答案" width="330">
                </el-table-column>
                <el-table-column prop="difficulty" label="难度" width="90">
                </el-table-column>
                <el-table-column prop="useCount" label="引用次数" width="90">
                </el-table-column>
                <el-table-column prop="type" label="类型" width="90">
                </el-table-column>
                <el-table-column prop="title" label="名称" width="100">
                </el-table-column>
              </el-table>
              <!-- 分页 -->
              <el-pagination
                :current-page="this.quesitonpage"
                :page-size="limit"
                :total="this.questiontotal"
                style="padding: 30px 0; text-align: center"
                layout="total, prev, pager, next, jumper"
                @current-change="fetchQuestion"
              />
            </div>
          </template>
          <el-row>
            <el-col :span="6" style="position: relative; left: 30px">
              <el-button @click="innerDrawer = false">取 消</el-button>
              <el-button @click="addquestionTopaper()" type="primary"
                >添 加</el-button
              >
            </el-col>
          </el-row>
        </el-drawer>
        <el-row>
          <el-col
            ><el-button
              style="position: relative; left: 600px"
              @click="addquestiondialog = false"
              >取 消</el-button
            ></el-col
          >
        </el-row>
      </div>
    </el-drawer>
    <el-dialog :visible.sync="dialogPaperFormVisible" title="添加试卷">
      <el-form :model="paper" label-width="150px">
        <el-form-item label="试卷大标题设置">
          <el-input v-model="paper.papertitle" />
        </el-form-item>
        <el-form-item label="试卷小标题设置">
          <el-input v-model="paper.testtitle" />
        </el-form-item>
        <el-form-item label="试卷适用专业设置">
          <el-input v-model="paper.classtitle" />
        </el-form-item>
        <!-- <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
        </el-form-item> -->
        <el-form-item label="做题时长/分钟">
          <el-input-number
            v-model="paper.time"
            :min="0"
            :step="1"
            change=""
            controls-position="right"
          />
        </el-form-item>
        <el-form-item label="难度">
          <el-rate
            v-model="paper.difficulty"
            :texts="diff"
            :colors="['#99A9BF', '#F7BA2A', '#F56c6c']"
            show-text
          >
          </el-rate>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogPaperFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="dialogTypeForm"
      v-if="dialogTypeForm"
      title="新增大题"
    >
      <el-form label-width="120px">
        <el-form-item label="类型">
          <el-select v-model="typeObj.questionTypeSysId">
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="大标题设置">
          <el-input v-model="typeObj.name" />
        </el-form-item>
        <el-form-item label="此大题分值">
          <el-input-number
            v-model="typeObj.score"
            :min="0"
            :step="0.5"
            change=""
            controls-position="right"
          />
        </el-form-item>
        <el-form-item label="题目数量">
          <el-input-number
            v-model="typeObj.number"
            :min="0"
            :step="1"
            change=""
            controls-position="right"
          />
        </el-form-item>
        <el-button
          :loading="loading"
          style="margin-left: 10px"
          size="small"
          type="success"
          @click="addType()"
          >添加大题到试卷</el-button
        >
      </el-form>
    </el-dialog>

    <el-dialog
      :visible.sync="dialogQuestionChoice"
      width="95%"
      he
      title="选择试题列表"
    >
      <el-button style="" @click="openquestionadd()" type="text"
        >增加习题</el-button
      >

      <el-table :data="paperchoiceList" style="width: 100%">
        <el-table-column label="序号" width="70" align="center">
          <template slot-scope="scope">
            {{ (choicepage - 1) * limit + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="content" label="题干" style="" min-width="300">
        </el-table-column>
        <el-table-column prop="answer" label="答案" width="100">
        </el-table-column>
        <el-table-column prop="choiceA" label="选项A" width="150">
        </el-table-column>
        <el-table-column prop="choiceB" label="选项B" width="150">
        </el-table-column>

        <el-table-column prop="choiceC" label="选项C" width="150">
        </el-table-column>
        <el-table-column prop="choiceD" label="选项D" width="150">
        </el-table-column>
        <el-table-column prop="difficulty" label="难度" width="90">
        </el-table-column>
        <el-table-column prop="useCount" label="引用次数" width="90">
        </el-table-column>
        <el-table-column prop="title" label="名称"> </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改</el-button
            >
            <el-button type="danger" size="mini" icon="el-icon-delete"
             @click="deletePaperChoiceQuestion(scope.row.typeid)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="this.paperchoicepage"
        :page-size="limit"
        :total="this.paperchoicetotal"
        style="padding: 30px 0; text-align: center"
        layout="total, prev, pager, next, jumper"
        @current-change="fetchPaperChoiceQuestion"
      />
    </el-dialog>
    <el-dialog :visible.sync="dialogQuestion" title="试题列表" width="95%">
      <el-button style="" @click="openquestionadd()" type="text"
        >增加习题</el-button
      >

      <el-table :data="paperquestions" style="width: 90%">
        <el-table-column label="序号" width="70" align="center">
          <template slot-scope="scope">
            {{ (quesitonpage - 1) * limit + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="content" label="题干" style="" min-width="400">
        </el-table-column>
        <el-table-column prop="answer" label="答案" width="330">
        </el-table-column>
        <el-table-column prop="difficulty" label="难度" width="90">
        </el-table-column>
        <el-table-column prop="useCount" label="引用次数" width="90">
        </el-table-column>
        <el-table-column prop="type" label="类型" width="90"> </el-table-column>
        <el-table-column prop="title" label="名称" width="100">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改</el-button
            >
            <el-button type="danger" size="mini" icon="el-icon-delete"
            @click="deletePaperQuestion(scope.row.typeid)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="this.paperquesitonpage"
        :page-size="limit"
        :total="this.paperquestiontotal"
        style="padding: 30px 0; text-align: center"
        layout="total, prev, pager, next, jumper"
        @current-change="fetchPaperQuestion"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogQuestion = false">关闭</el-button>
      </div>
    </el-dialog>
    
    <el-dialog :visible.sync="dialogAutoPaperFormVisible" title="自动生成试卷" width="80%">
      <el-form :model="paper" label-width="150px">
        <el-form-item label="试卷大标题设置">
          <el-input v-model="paper.papertitle" />
        </el-form-item>
        <el-form-item label="试卷小标题设置">
          <el-input v-model="paper.testtitle" />
        </el-form-item>
        <el-form-item label="试卷适用专业设置">
          <el-input v-model="paper.classtitle" />
        </el-form-item>
        <el-form-item label="做题时长/分钟">
          <el-input-number
            v-model="paper.time"
            :min="0"
            :step="1"
            change=""
            controls-position="right"
          />
        </el-form-item>
         <el-divider></el-divider>
          <span style=" font-size: 20px"> 习题数量难易度设置</span>
       
 <div v-for="(chapter,index) in chapterAndInfo" :key="chapter.id" >
            <el-divider></el-divider>
              <span style=" font-size: 15px">第{{index+1}}章：{{chapter.chapter.title}}</span>
            <el-divider></el-divider>
            <div v-for="node in chapter.list" :key="chapter.list.id" >            
              <span style=" font-size: 10px"> {{node.sys.name }}:{{node.num}}</span>
            </div>
            <!-- <el-form-item label="难度">
              <el-rate
                v-model="chapter.difficulty"
                :texts="diff"
                :colors="['#99A9BF', '#F7BA2A', '#F56c6c']"
                show-text>
              </el-rate>
            </el-form-item> -->
            <!-- <el-form-item label="习题数量">
              <el-input-number v-model="chapter.number" :min="0" :step="1" controls-position="right"/>
            </el-form-item>
            <el-form-item label="单题分值" >
              <el-input-number v-model="chapter.score" :step="0.5" :min="0" controls-position="right"/>
            </el-form-item> -->
          </div>
      
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAutoPaperFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAutoPaperOrUpdate">确 定</el-button>
      </div>
    </el-dialog>

     <el-dialog :visible.sync="openDownloadDialog" v-if="openDownloadDialog"
      title="生成试卷下载" width="60%">
        <el-form >
        <el-form-item label="试卷大标题设置">
          <el-input v-model="paperInfo.papertitle" />
        </el-form-item>
        <el-form-item label="试卷小标题设置">
          <el-input v-model="paperInfo.testtitle" />
        </el-form-item>
        <el-form-item label="试卷适用专业设置">
          <el-input v-model="paperInfo.classtitle" />
        </el-form-item>
      </el-form>
      <el-button type="primary" size="mini" @click="clearPaper">清除</el-button>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleDownLoad()">生成</el-button>
        <el-button @click="openDownloadDialog=false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import course from "api/course";
import paper from "api/paper";
import questiontype from "api/questiontype";
import question from "api/question";
import file from "api/file";
import chapter from 'api/edu/chapter'
export default {
  components: {
    chapter_course: () => import("views/Course/publish_course/chapter.vue"),
    publishCourse: () => import("views/Course/publish_course/publish.vue"),
  },
  data() {
    return {
      page: 1,
      total: 0,
      questiontotal: 0, // 总记录数
      quesitonpage: 1, // 页码
      limit: 10,
      choicetotal: 0, // 总记录数
      choicepage: 1, // 页码
      paperquestiontotal: 0, // 总记录数
      paperquesitonpage: 1, // 页码
      limit: 10,
      paperchoicetotal: 0, // 总记录数
      paperchoicepage: 1, // 页码

      diff: ["无聊", "容易", "中等", "费时", "极难"],
      typeList: [],
      questionList: [],
      coursedata: [],
      searchObj: {}, // 查询表单对象
      subSubjectList: [],
      subjectNestedList: [],
      paperList: [],
      paper: {
        papertitle: "2017-2018学年第一学期期末考试试卷",
        testtitle: "《操作系统》（A）卷",
        classtitle: "（适用专业班级：网工15级①②班）",
   
        courseId: "",
        time: 90,
        difficulty: 0,
        sort: 0,
        courseId: "",
      },
      typeObj: {
        paperId: "",
        typeSys: "",
        courseId: "",
        name: "（请将正确答案的代码填写在相应的框内。）",
        score: 10,
        number: 10,
      },
      preItem: {
        menuId: "course_31",
        menuNameCn: "课程大纲",

        iconCls: "el-icon-location",
        children: [],
        comp: "chapter_course",
        url: "/chapter_course",
      },
      nextItem: {
        menuId: "course_32",
        menuNameCn: "发布课程",
        iconCls: "el-icon-location",
        children: [],
        comp: "publishCourse",
        url: "/publishCourse",
      },
      dialogQuestion: false,
      dialogQuestionChoice: false,
      innerDrawer: false,
      addquestiondialog: false,
      saveBtnDisabled: false, // 保存按钮是否禁用
      courseId: "",
      dialogPaperFormVisible: false,
      dialogAutoPaperFormVisible:false,
      typeId: "",

      queryQuestionByTypeSys: {
        courseId: "",
        typeSys: "",
        typeId: "",
      },
      typeSys: "",
       openDownloadDialog:false,
      dialogQuestionChoice: false,
      dialogQuestion: false,
      options: [],
      dialogTypeForm: false,
      loading: false,
      multipleSelection: [{ id: "", sort: 0 }],
      tableData: [],
      oneIndex: -1,
      twoIndex: -1,
      questions: [],
      choiceList: [],
      choiceSelection: [],
      questionSelection: [],
      paperquestions: [],
      chapterAndInfo:[],
      paperchoiceList: [],
        paperInfo:{
        paperId:'',
         teaId:'',
         papertitle:'2017-2018学年第一学期期末考试试卷',
         testtitle:'《操作系统》（A）卷',
         classtitle:'（适用专业班级：网工15级①②班）',
         createStatus:2
       },
    };
  },

  created() {
    if (this.$store.state.course.currentCourseId != "none") {
      this.courseId = this.$store.state.course.currentCourseId;
      this.getPaperList(this.courseId);
    }
    this.initSubject();
    this.getTypeList();
  },
  watch: {
    isFollow(newVal, oldVal) {
      this.courseId = newVal;
    },
  },
  computed: {
    isFollow() {
      return this.$store.state.course.currentCourseId; //需要监听的数据
    },
    ...mapGetters([
      // https://vuex.vuejs.org/zh-cn/getters.html
      "getCourseCurrentTab",
      "getCourseTabs",
    ]),
  },
  methods: {
    ...mapActions(["courseAddTab", "courseRemoveTab"]),
    clearPaper(){
      this.paperInfo={}
    },
     getChapterAndInfo() {
     chapter.getChapterAndInfo(this.courseId).then(res=>{
       this.chapterAndInfo=res.data.data.list
     })
   
    },
    handleDownLoad() {
      this.paperInfo.teaId=this.$store.state.userinfo.id
     
        file.downCoursePaperWord(this.paperInfo)
          .then((res) => {
            if (res) {
              let blob = new Blob([res.data], {
                type: "application/octet-stream;charset=utf-8",
              }); // res就是接口返回的文件流了
              let objectUrl = URL.createObjectURL(blob);
              const elink = document.createElement("a");
              elink.download = this.paperInfo.papertitle + ".doc"; //下载文件名称,
              elink.style.display = "none";
              elink.href = objectUrl;
              document.body.appendChild(elink);
              elink.click();
              URL.revokeObjectURL(elink.href); // 释放URL 对象
              document.body.removeChild(elink);
            }
  
          })
          .catch(function (error) {
            console.log(error);
          });

      },
  
    downloadPaperWord(id){
      this.paperInfo.createStatus=2
     this.openDownloadDialog=true
     this.paperInfo.paperId=id
    },
    getQuestionsByPaperTypeId(id) {
      this.queryQuestionByTypeSys.typeId = id;
      this.fetchPaperQuestion();
    },
    
    fetchPaperQuestion(page = 1) {
      this.paperquesitonpage = page;
      question
        .getQuestionsByPaperTypeId(
          this.paperquesitonpage,
          10,
          this.queryQuestionByTypeSys
        )
        .then((res) => {
          this.paperquestions = res.data.data.list;

          this.paperquestiontotal = res.data.data.total;
        });
    },
    fetchPaperChoiceQuestion(page = 1) {
      this.paperchoicepage = page;
      question
        .getChoiceQuestionsByPaperTypeId(
          this.paperchoicepage,
          10,
          this.queryQuestionByTypeSys
        )
        .then((res) => {
          this.paperchoiceList = res.data.data.list;
          this.paperchoicetotal = res.data.data.total;
        });
    },
    getChoiceQuestionsByPaperTypeId(id) {
      this.queryQuestionByTypeSys.typeId = id;
      this.fetchPaperChoiceQuestion();
    },

    openQuestionDialog(type) {
      this.queryQuestionByTypeSys.typeSys = type.questionTypeSysId;
      this.queryQuestionByTypeSys.typeId = type.id;
      this.typeId = type.id;
      if (type.questionTypeSysId == 1) {
        this.dialogQuestionChoice = true;
        this.fetchPaperChoiceQuestion();
      } else {
        this.dialogQuestion = true;
        this.fetchPaperQuestion();
      }
    },
    getTypeList() {
      if (this.typeList[0] == null) {
        questiontype.getSysType().then((res) => {
          this.typeList = res.data.data.list;
        });
        this.getPaperList(this.courseId);
      }
    },
    addType() {
      this.typeObj.courseId = this.courseId;
      paper.addPaperSysType(this.typeObj);
      this.dialogTypeForm = false;
      this.getPaperList(this.courseId);
    },
    openTypedialog(id) {
      this.typeObj.paperId = id;
      this.dialogTypeForm = true;
    },

    saveOrUpdate() {
      this.paper.courseId = this.courseId;
      if (!this.paper.id) {
        this.addPaper();
      } else {
        this.updatePaper();
      }
    },
      saveAutoPaperOrUpdate() {
      this.paper.courseId = this.courseId;
      if (!this.paper.id) {
        this.addPaper();
      } else {
       // this.updatePaper();
      }
    },

    addquestionTopaper() {
      if (
        this.questionSelection[0] != null ||
        this.choiceSelection[0] != null
      ) {
        let list = [];
        if (this.queryQuestionByTypeSys.typeSys == 1) {
           console.log(this.choiceSelection);
          this.choiceSelection.forEach((e) => {
            list.push(e.id);
          });
          questiontype
            .addChoiceQuestionListAndpaperType(list, this.typeId)
            .then((res) => {
              this.$message({
                type: "success",
                message: "添加成功!",
              });
            });
        } else {
            this.questionSelection.forEach((e) => {
            list.push(e.id);
          });
       
          questiontype
            .addQuestionListAndpaperType(list, this.typeId)
            .then((res) => {
              this.$message({
                type: "success",
                message: "添加成功!",
              });
            });
        }
        this.innerDrawer = false;
      } else {
        this.$message({
          type: "fail",
          message: "添加失败!",
        });
      }
    },
    handleChoiceSelectionChange(val) {
      this.choiceSelection = val;
    },
    handlequestionSelectionChange(val) {
      this.questionSelection = val;
    },
    getPaperList(id) {
      paper.getPaperListByCourseId(id).then((res) => {
        this.paperList = res.data.data.list;
      });
    },
    addPaper() {
      paper.addPaper(this.paper).then((res) => {
        this.$message({
          type: "success",
          message: "添加试卷成功!",
        });
        this.getPaperList(this.courseId);
      });
      this.dialogPaperFormVisible = false;
    },
    updatePaper() {},
    deletePaperById(id) {
      this.$confirm("此操作将删除试卷, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        paper.deletePaper(id).then((res) => {
          this.$message({
            type: "success",
            message: "删除试卷成功!",
          });
        });
        this.getPaperList(this.courseId);
      });
    },
    deletePaperTypeById(id) {
      this.$confirm("此操作将删除试卷, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        paper.deletePaperSysType(id).then((res) => {
          this.$message({
            type: "success",
            message: "删除试卷成功!",
          });
        });
      });
    },
     deletePaperChoiceQuestion(id){
      this.$confirm('此操作将删除试题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {  //点击确定，执行then方法
        //调用删除的方法
 
        question.deletePaperChoiceQuestionAndMid(id)
          .then(response =>{//删除成功
            //提示信息
            this.$message({
              type: 'success',
              message: '删除试题成功!'
            });
            //刷新页面
         
this.fetchPaperChoiceQuestion(this.paperchoicepage)       
            // this.getQuestions(this.chapterId)
           
          })
      }) //点击取消，执行catch方法

    },
     deletePaperQuestion(id){
      this.$confirm('此操作将删除试题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {  //点击确定，执行then方法
        //调用删除的方法
 
        question.deletePaperQuestionAndMid(id)
          .then(response =>{//删除成功
            //提示信息
            this.$message({
              type: 'success',
              message: '删除试题成功!'
            });
            //刷新页面
       
this.fetchPaperQuestion(this.paperquesitonpage)       
            // this.getQuestions(this.chapterId)
           
          })
      }) //点击取消，执行catch方法

    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          // this.getfolderQuestion(this.currentfolderId)
        })
        .catch((_) => {});
    },
    gotoPage(page) {
      course.getCourseList(page, 8, this.searchObj).then((response) => {
        this.coursedata = response.data.data.items;
      });
    },
    searchOne(subjectParentId, index) {
      //把传递index值赋值给oneIndex,为了active样式生效
      this.oneIndex = index;
      this.twoIndex = -1;
      this.searchObj.subjectId = "";
      this.subSubjectList = [];
      //把一级分类点击id值，赋值给searchObj
      this.searchObj.subjectParentId = subjectParentId;
      //点击某个一级分类进行条件查询
      this.gotoPage(1);
      //拿着点击一级分类id 和 所有一级分类id进行比较，
      //如果id相同，从一级分类里面获取对应的二级分类
      for (let i = 0; i < this.subjectNestedList.length; i++) {
        //获取每个一级分类
        var oneSubject = this.subjectNestedList[i];
        //比较id是否相同
        if (subjectParentId == oneSubject.id) {
          //从一级分类里面获取对应的二级分类
          this.subSubjectList = oneSubject.children;
        }
      }
    },
    //5 点击某个二级分类实现查询
    searchTwo(subjectId, index) {
      //把index赋值,为了样式生效
      this.twoIndex = index;
      //把二级分类点击id值，赋值给searchObj
      this.searchObj.subjectId = subjectId;
      //点击某个二级分类进行条件查询
      this.gotoPage(1);
    },
    //6 根据销量排序
    searchBuyCount() {
      //设置对应变量值，为了样式生效
      this.buyCountSort = "1";
      this.gmtCreateSort = "";
      this.priceSort = "";
      //把值赋值到searchObj
      this.searchObj.buyCountSort = this.buyCountSort;
      this.searchObj.gmtCreateSort = this.gmtCreateSort;
      this.searchObj.priceSort = this.priceSort;
      //调用方法查询
      this.gotoPage(1);
    },
    openPaperDialog() {
      //弹框
      this.dialogPaperFormVisible = true;
    },
      openAutoPaperDialog() {
      //弹框
     this.getChapterAndInfo() 
      this.dialogAutoPaperFormVisible = true;
    },
    openquestionadd() {
      this.addquestiondialog = true;
    },
    initSubject() {
      course.getAllSubject().then((response) => {
        this.subjectNestedList = response.data.data.items;
        //  this.searchOne(response.data.data.items[0].id,0)
      });
    },
    previous() {
      this.courseAddTab(this.preItem);
      this.courseRemoveTab("course_33");
    },
    next() {
      this.courseAddTab(this.nextItem);
      this.courseRemoveTab("course_33");
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    fetchChoiceQuestion(page = 1) {
      question
        .GetChoiceQuestionsByQuestionTypeSys(
          this.choicepage,
          this.limit,
          this.queryQuestionByTypeSys
        )
        .then((res) => {
          this.choiceList = res.data.data.list;
          this.choicetotal = res.data.data.total;
        });
    },
    fetchQuestion(page = 1) {
      question
        .GetQuestionsByQuestionTypeSys(
          this.quesitonpage,
          this.limit,
          this.queryQuestionByTypeSys
        )
        .then((res) => {
          this.questions = res.data.data.list;
          this.questiontotal = res.data.data.total;
        });
    },
    getcourseInfo(courseId) {
      this.multipleSelection = [];
      this.innerDrawer = true;
      this.queryQuestionByTypeSys.courseId = courseId;

      if (this.queryQuestionByTypeSys.typeSys == 1) {
        this.fetchChoiceQuestion();
      } else {
        this.fetchQuestion();
      }
    },
  },
};
</script>

<style scoped>
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  float: left;
  font-size: 25px;
  margin: 25px 3;
  padding: 10px;
  height: 25px;
  line-height: 30px;
  width: 90%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 20px;
}

.videoList {
  padding-left: 80px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 40px;
  line-height: 30px;
  width: 85%;
  border: 1px dotted #ddd;
}
.paperList {
  padding-left: 100px;
}
.paperList p {
  height: 55px;
}
</style>
